import type { IUserInfoRes } from './types/login'
import http from '@/utils/request'

export interface IRegisterForm {
  avatarUrl?: string
  nickName?: string
  childName?: string
  fileId?: string
  code: string
}

/**
 * 获取微信登录凭证
 * @returns Promise 包含微信登录凭证(code)
 */
export function getWxCode() {
  return new Promise<UniApp.LoginRes>((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success: res => resolve(res),
      fail: err => reject(new Error(err)),
    })
  })
}

export async function wxLogin(data: Pick<IRegisterForm, 'code'>): Promise<string> {
  return http<IRegisterForm, string>('auth/login', data)
}

export async function register(data: IRegisterForm): Promise<string> {
  return http<IRegisterForm, string>('auth/register', data)
}

export async function updateInfo(data: { avatarUrl?: string, nickName?: string }) {
  return http('user/update', data)
}

export async function getUserInfo(): Promise<IUserInfoRes> {
  return http('user/info')
}
